﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace PoemonAdventures
{
    class FightScreen
    {
        bool playerTurn = true;
        bool enemyTurn = false;
        int enemyHealth = 100;
        int playerHealth = 100;

        String att = ("Normal Attack");
        String att2 = ("Psychic Attack");
        String att3 = ("Force Attack");
        String whaa = ("What to do now");
        String whaa2 = ("You Won");
        String whaa4 = ("You Died");
        Color farg = Color.White;
        Color farg2 = Color.White;
        Color farg3 = Color.White;
        int menyVal = 1;
        int timer = 0;
        int enterTimer = 0;
        Boolean able = true;
        Boolean enterAble = true;
        int actionTimer = 0;
        Boolean actionBool = true;

        enum FightState
        {
            Fight,
            Lost,
            Win
        }

        FightState fightState = FightState.Fight;

        KeyboardState keyState;
        KeyboardState prevKeyState;
        Player player;
        Poemon enemy;

        public FightScreen(Player player, Poemon enemy)
        {
            this.player = player;
            this.enemy = enemy;
        }

        public void Update(GameTime gameTime)
        {
            prevKeyState = keyState;
            keyState = Keyboard.GetState();
            if (keyState.IsKeyDown(Keys.Back) && keyState != prevKeyState)
            {
                ScreenManager.gameState = GameState.Map;
            }
            if (playerTurn == false)
            {
                enterAble = false;
                enemyTurn = true;
            }
            if (enemyTurn == true && enemyHealth > 0)
            {
                playerHealth -= enemy.NormalAttack();
                playerTurn = true;
                enemyTurn = false;
                enterAble = true;
            }
            timer++;
            enterTimer++;
            KeyboardState kS = Keyboard.GetState();
            if (timer >= 40)
            {
                timer = 0;
                able = true;
            }
            if (menyVal < 1)
            {
                menyVal = 3;
            }
            if (menyVal > 3)
            {
                menyVal = 1;
            }
            if (kS.IsKeyDown(Keys.Up) && able == true)
            {
                menyVal -= 1;
                able = false;
            }
            if (kS.IsKeyDown(Keys.Down) && able == true)
            {
                menyVal += 1;
                able = false;
            }

            if (kS.IsKeyDown(Keys.Enter) && enterAble == true && actionBool == true)
            {

                enterAble = false;
                if (menyVal == 1)
                {
                    whaa = ("You used Normal attack");
                    enemyHealth -= player.myPoemon.NormalAttack();
                    enemyTurn = true;
                    playerTurn = false;
                    actionBool = false;

                }
                if (menyVal == 2)
                {
                    whaa = ("You used Psychic attack");
                    enemyHealth -= player.myPoemon.PowerAttack();
                    enemyTurn = true;
                    playerTurn = false;
                    actionBool = false;
                }

                if (menyVal == 3)
                {
                    whaa = ("You used Force attack");
                    enemyHealth -= player.myPoemon.ForceAttack();
                    enemyTurn = true;
                    playerTurn = false;
                    actionBool = false;
                }

            }
            if (menyVal == 1)
            {
                farg = Color.Green;
                farg2 = Color.White;
                farg3 = Color.White;
            }
            if (menyVal == 2)
            {
                farg = Color.White;
                farg2 = Color.Green;
                farg3 = Color.White;
            }
            if (menyVal == 3)
            {
                farg = Color.White;
                farg2 = Color.White;
                farg3 = Color.Green;
            }
            if (actionTimer >= 60)
            {
                actionBool = true;
                actionTimer = 0;

            }
            if (actionBool == false)
            {
                actionTimer++;
            }
            if (playerHealth == 0)
            {
                fightState = FightState.Lost;
                enterAble = false;
            }
            if (enemyHealth == 0)
            {
                fightState = FightState.Win;
                enterAble = false;
            }
            if (playerHealth < 0)
            {
                playerHealth = 0;
            }
            if (enemyHealth < 0)
            {
                enemyHealth = 0;
            }
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Begin();
            switch (fightState)
            {
                case FightState.Fight:
                    spriteBatch.DrawString(ScreenManager.osdFont, "Enemy" + " " + enemyHealth, new Vector2(20, 20), Color.Tomato);
                    spriteBatch.DrawString(ScreenManager.osdFont, "Player" + " " + playerHealth, new Vector2(20, 40), Color.Green);
                    spriteBatch.DrawString(ScreenManager.osdFont, att, new Vector2(100, 150), farg);
                    spriteBatch.DrawString(ScreenManager.osdFont, att2, new Vector2(100, 200), farg2);
                    spriteBatch.DrawString(ScreenManager.osdFont, att3, new Vector2(100, 250), farg3);
                    spriteBatch.DrawString(ScreenManager.osdFont, whaa, new Vector2(500, 150), Color.White);
                    break;
                case FightState.Win:
                    spriteBatch.DrawString(ScreenManager.osdFont, whaa2, new Vector2(500, 250), Color.White);
                    break;
                case FightState.Lost:
                    spriteBatch.DrawString(ScreenManager.osdFont, whaa4, new Vector2(500, 350), Color.White);
                    break;
            }
            spriteBatch.End();
        }
    }
}
